<%--
  Created by IntelliJ IDEA.
  User: lxm
  Date: 2018/5/3
  Time: 下午5:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <jsp:include page="/static/common/stylesheets.jsp"></jsp:include>
    <jsp:include page="nav.jsp"></jsp:include>

    <title>API文档说明</title>
    <style>
        p {
            font-size: 16px;
            line-height: 2;
        }

        .icon {
            position: relative;
            font-size: 15px;
            height: 32px;
            display: block;
            fill: rgba(51, 51, 51, 0.5);
            margin-right: 20px;
            -webkit-transition: all .2s ease-out;
            transition: all .2s ease-out;
        }

        .icon.active {
            fill: #E74C3C;
        }

        .icon.big {
            width: 64px;
            height: 64px;
            fill: rgba(51, 51, 51, 0.5);
        }

        #wrapper {
            min-height: 700px;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            background-color: #fff;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
            display: -webkit-box;
            display: -webkit-flex;
            display: -ms-flexbox;
            display: flex;
            -webkit-box-align: center;
            -webkit-align-items: center;
            -ms-flex-align: center;
            align-items: center;
            -webkit-box-pack: left;
            -webkit-justify-content: left;
            -ms-flex-pack: left;
            justify-content: left;

        }

        #left-side {
            height: 70%;
            width: 15%;
            display: -webkit-box;
            display: -webkit-flex;
            display: -ms-flexbox;
            display: flex;
            -webkit-box-align: center;
            -webkit-align-items: center;
            -ms-flex-align: center;
            align-items: center;
            -webkit-box-pack: center;
            -webkit-justify-content: center;
            -ms-flex-pack: center;
            justify-content: center;
        }

        #left-side ul li {
            padding-top: 10px;
            padding-bottom: 10px;
            display: -webkit-box;
            display: -webkit-flex;
            display: -ms-flexbox;
            display: flex;
            line-height: 34px;
            color: rgba(51, 51, 51, 0.5);
            font-weight: 500;
            cursor: pointer;
            -webkit-transition: all .2s ease-out;
            transition: all .2s ease-out;
        }

        #left-side ul li:hover {
            color: #333333;
            -webkit-transition: all .2s ease-out;
            transition: all .2s ease-out;
        }

        #left-side ul li:hover > .icon {
            fill: #333;
        }

        #left-side ul li.active {
            color: #333333;
        }

        #left-side ul li.active:hover > .icon {
            fill: #E74C3C;
        }

        #border {
            height: 600px;
            width: 1px;
            background-color: rgba(51, 51, 51, 0.2);
        }

        #border #line {
            width: 5px;
            height: 54px;
            background-color: #E74C3C;
            margin-left: -2px;
            margin-top: 7px;
            -webkit-transition: all .4s ease-in-out;
            transition: all .4s ease-in-out;
        }

        #right-side {
            /*height: 300px;*/
            min-height: 650px;
            width: 85%;
            /*overflow: hidden;*/
        }

        #right-side #first, #right-side #second, #right-side #third, #right-side #fourth {
            position: absolute;
            height: 300px;
            width: 75%;
            -webkit-transition: all .6s ease-in-out;
            transition: all .6s ease-in-out;
            margin-top: -350px;
            opacity: 0;
            display: -webkit-box;
            display: -webkit-flex;
            display: -ms-flexbox;
            display: flex;
            -webkit-box-align: center;
            -webkit-align-items: center;
            -ms-flex-align: center;
            align-items: center;
            -webkit-box-pack: center;
            -webkit-justify-content: center;
            -ms-flex-pack: center;
            justify-content: center;
            -webkit-box-orient: vertical;
            -webkit-box-direction: normal;
            -webkit-flex-direction: column;
            -ms-flex-direction: column;
            flex-direction: column;
        }

        #right-side #first.active, #right-side #second.active, #right-side #third.active, #right-side #fourth.active {
            margin-top: 0px;
            opacity: 1;
            -webkit-transition: all .6s ease-in-out;
            transition: all .6s ease-in-out;
        }

        .tab-active {
            color: #333333;
            margin-top: 0px;
            opacity: 1;
            -webkit-transition: all .6s ease-in-out;
            transition: all .6s ease-in-out;
        }

        .tab-content-active {
            margin-top: 0px;
            opacity: 1;
            -webkit-transition: all .6s ease-in-out;
            transition: all .6s ease-in-out;
        }
    </style>
</head>
<body>
<jsp:include page="/static/common/header.jsp"></jsp:include>

<div class="layui-main site-inline">
    <jsp:include page="/static/common/docs.jsp"></jsp:include>

    <div class="site-content">
        <h1 class="site-h1">简介 - 平台API指南</h1>
        <p>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本自然语言处理与数据可视化平台，封装了HanLP与Ansj等Java自然语言处理算法包的接口，并运行在Apache
            Tomcat服务器上，以RESTful
            API的形式对外提供自然语言处理服务，并以JQuery库、D3.js库、百度Echarts图表库等作为web前端数据可视化的基础。采用了目前主流的SSH框架作为web应用程序的核心框架，并选择了开源、安全、稳定的MySQL数据库。Hibernate框架对于MySQL有着良好的技术支持，使数据交互更加简便。
        </p>
        <fieldset class="layui-elem-field layui-field-title site-title">
            <legend><a name="compatibility">兼容性和面向场景</a></legend>
        </fieldset>
        <p>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本自然语言处理与可视化平台，使用HTTP协议作为数据传输方式，兼容Java、C/C++、Python等不同类型的语言，用户仅需发起HTTP调用即可获取到接口响应。可以使用<a
                href="#">POST MAN</a>等工具调试接口。 其面向于一些对自然语言处理有需求的、服务器容量小，不想配置复杂环境的个人开发者或中小企业。
        </p>

        <fieldset class="layui-elem-field layui-field-title site-title">
            <legend><a name="get">调用示例</a></legend>
        </fieldset>
        <blockquote class="layui-elem-quote" style="text-align: center;">
            以调用分词接口为例
        </blockquote>
        <div class="layui-tab">
            <ul class="layui-tab-title">
                <li class="layui-this">Java</li>
                <li>C/C++</li>
                <li>Python</li>
                <li>Crul</li>
                <li>Ruby</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <pre>
                        <code class="java hlsj">
/**
     * 发送HttpPost请求
     *
     * @param strURL
     *            服务地址
     * @param params
     *
     * @return 成功:返回json字符串<br/>
     */
    public static String jsonPost(String strURL, Map<String, String> params) {
        try {
            URL url = new URL(strURL);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST"); // 设置请求方式
            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.connect();
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
            out.append(JSONUtil.object2JsonString(params));
            out.flush();
            out.close();

            int code = connection.getResponseCode();
            InputStream is = null;
            if (code == 200) {
                is = connection.getInputStream();
            } else {
                is = connection.getErrorStream();
            }

            // 读取响应
            int length = (int) connection.getContentLength();// 获取长度
            if (length != -1) {
                byte[] data = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, data, destPos, readLen);
                    destPos += readLen;
                }
                String result = new String(data, "UTF-8"); // utf-8编码
                return result;
            }

        } catch (IOException e) {
            LOG.error("Exception occur when send http post request!", e);
        }
        return "error"; // 自定义错误信息
    }
                        </code>
                    </pre>
                </div>
                <div class="layui-tab-item">
                    <pre>
                        <code class="hlsj c">
#include "stdio.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "time.h"
#include "errno.h"
#include "signal.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "sys/wait.h"
#include "sys/time.h"
#include "netinet/in.h"
#include "arpa/inet.h"

#define PORT 80
#define BUFSIZE 1024

int main(int argc, char **argv)
{
        int sockfd, ret, i, h;
        struct sockaddr_in servaddr;
        char str1[4096], str2[4096], buf[BUFSIZE], *str;
        socklen_t len;
        fd_set   t_set1;
        struct timeval  tv;

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
                printf("创建网络连接失败,本线程即将终止---socket error!\n");
                exit(0);
        };

        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(PORT);
        if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){
                printf("创建网络连接失败,本线程即将终止--inet_pton error!\n");
                exit(0);
        };

        if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
                printf("连接到服务器失败,connect error!\n");
                exit(0);
        }
        printf("与远端建立了连接\n");

        memset(str2, 0, 4096);
        strcat(str2, {"cunstom_filter":"我","seg_type":"HANLP_NLP","stopwords_filter":false,"punctuation_filter":false,"format":"JSON","text":[{"text":"我爱桂林电子科技大学"}]});
        str=(char *)malloc(128);
        len = strlen(str2);
        sprintf(str, "%d", len);

        memset(str1, 0, 4096);
        strcat(str1, "POST /api/analysis/segment HTTP/1.1\n");
        strcat(str1, "Host: http://myweb.com\n");
        strcat(str1, "Content-Type: application/json\n");
        strcat(str1, "Content-Length: ");
        strcat(str1, str);
        strcat(str1, "\n\n");
        //str2的值为post的数据
        strcat(str1, str2);
        strcat(str1, "\r\n\r\n");
        printf("%s\n",str1);

        ret = write(sockfd,str1,strlen(str1));
        if (ret < 0) {
                printf("发送失败！错误代码是%d，错误信息是'%s'\n",errno, strerror(errno));
                exit(0);
        }else{
                printf("消息发送成功，共发送了%d个字节！\n\n", ret);
        }

        FD_ZERO(&t_set1);
        FD_SET(sockfd, &t_set1);

        tv.tv_sec= 0;
        tv.tv_usec= 0;
        h= 0;

        while(1){
                sleep(2);
                printf("--------------->1");
                h = select(sockfd +1, &t_set1, NULL, NULL, &tv);
                printf("--------------->2");
                //if (h == 0) continue;
                if (h == -1) {
                        close(sockfd);
                        printf("在读取数据报文时SELECT检测到异常，该异常导致线程终止！\n");
                        return -1;
                };
                if ( FD_ISSET(sockfd, &t_set1) ){
                        memset(buf, 0, 4096);
                        i= read(sockfd, buf, 4095);
                        if (i==0){
                                close(sockfd);
                                printf("读取数据报文时发现远端关闭，该线程终止！\n");
                                return -1;
                        }
                        printf("%s\n", buf);
                }
        }
        close(sockfd);


        return 0;
}
                        </code>
                    </pre>
                </div>
                <div class="layui-tab-item">
                    <pre><code class="python hlsj code-response" style="">
from urllib import parse,request
import json
textmod={"cunstom_filter":"我","seg_type":"HANLP_NLP","stopwords_filter":false,"punctuation_filter":false,"format":"JSON","text":[{"text":"我爱桂林电子科技大学"}]}
#json串数据使用
textmod = json.dumps(textmod).encode(encoding='utf-8')
#普通数据使用
textmod = parse.urlencode(textmod).encode(encoding='utf-8')
#header可以忽略不使用
header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',"Content-Type": "application/json"}
url='http://myweb.com/api/analysis/segment'
req = request.Request(url=url,data=textmod,headers=header_dict)
res = request.urlopen(req)
res = res.read()
print(res)
#输出内容:
print(res.decode(encoding='utf-8'))
#输出内容


                    </code> </pre>
                </div>
                <div class="layui-tab-item">
                    <pre>
                        <code class="hljs crul">
curl -l -H "Content-type: application/json" -X POST -d '{"cunstom_filter":"我","seg_type":"HANLP_NLP","stopwords_filter":false,"punctuation_filter":false,"format":"JSON","text":[{"text":"我爱桂林电子科技大学"}]}' http://myweb.com/api/analysis/segment
                        </code>
                    </pre>
                </div>
                <div class="layui-tab-item">
                    <pre>
                        <code class="hljs ruby">
equire 'net/http'
require 'uri'
require 'json'
url='http://myweb.com/api/analysis/segment'
port=80
customers = {"cunstom_filter":"我","seg_type":"HANLP_NLP","stopwords_filter":false,"punctuation_filter":false,"format":"JSON","text":[{"text":"我爱桂林电子科技大学"}]}.to_json

def send_data(url,data)
    url = URI.parse(url)
    req = Net::HTTP::Post.new(,{'Content-Type' => 'application/json'})
    req.body = data
    res = Net::HTTP.new(url.host,url.port).start{|http| http.request(req)}

    puts res.body
end
                        </code>
                    </pre>
                </div>
            </div>
        </div>

        <div class="layui-elem-quote">
            <p>自然语言处理与可视化平台 - 林晓民</p>
        </div>

    </div>

        <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="/static/common/footer.jsp"></jsp:include>
    <jsp:include page="/static/common/scripts.jsp"></jsp:include>
    <script src="/static/layui/js/doc/doc.js"></script>
</body>
</html>